<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%
	String basePath = request.getScheme()+"://"+request.getServerName()
		+ ":"+ request.getServerPort() + request.getContextPath() + "/";
%>
 
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath %>">
<meta charset="UTF-8">
<title>修改员工信息</title>
</head>
<body>
<h1>修改员工信息</h1>
<form action="emp/modify.do" method="post" enctype="multipart/form-data">
<input type="hidden" name="eid" value="${eid }"><br>
姓名:<input type="text" name="ename"><br>
电话:<input type="text" name="ephone"><br>
部门:<select name="did">
</select><br>
生日:<input type="date" name="ebirthday"><br>
照片:<img width="60px"><br>
<input type="file" name="file"><br>
<input type="submit" value="修改">
${error }
</form>
	<script type="text/javascript" src="js/jquery-2.1.4.js"></script>
	<script type="text/javascript">
		//获取原来的信息
		function getEmp(){
			$.ajax({
				url: "emp/get.json",
				data: {eid: $("input[name='eid']").val()},
				type: "post",
				dataType: "json",
				success: function(data){
					$("input[name='ename']").val(data.ename);
					$("input[name='ephone']").val(data.ephone);
					$("select[name='did']").val(data.did);
					$("input[name='ebirthday']").val(data.ebirthday);
					if(data.photo != null){
						$("img").attr("src","upload/"+data.photo);
					}
				},
				error: function(){
					console.log("请求失败");
				}
			});
		}
		
		//获取部门信息
		$.ajax({
			url: "dept/getAll.json",
			data: {},
			type: "post",
			dataType: "json",
			success: function(data){
				var html = "";
				$.each(data,function(index,item){
					html += "<option value='"+item.did+"'>"+item.dname+"</option>";
				});
				$("select[name='did']").html(html);
				getEmp();
			},
			error: function(){
				console.log("请求失败");
			}
		});
		
		//上传图片的预览
		$("input[name='file']").change(function(){
			//1.获取选择的图片
			var filePath = $(this).val();
			//2.转换成可以在本地预览的格式
			var src = window.URL.createObjectURL(this.files[0]);
			//3.显示到img上
			$("img").attr("src",src);
		});
	</script>
</body>
</html>